#!/usr/bin/perl -w

# fix_mmp_gcce.pl
# ===============
#
# Author: Ricardo Mendonca Ferreira - ric@mpcnet.com.br
# Date..: 2010-10-21
#
# This script scans the .mmp files, add a GCCE condition to use the correct
# .def file and a EPOCALLOWDLLDATA statement to get rid of the
# "kf__camera.pyd contains uninitialized writable data." error.
#
# First, of course, backup your files!
# "c:\Program Files\WinRAR\WinRAR.exe" a -r mmp.rar *.mmp
#
# References:
#  http://discussion.forum.nokia.com/forum/showthread.php?160472-S60-5th-ed-claims-there-s-writable-static-data-in-a-dll-all-earlier-SDK-s-don-t
#  http://developer.symbian.org/wiki/Symbian_C++_Knowledgebase_Q&As#How_can_I_find_the_.22uninitialised_data.22_in_my_DLL.3F
#
# Run this at the root of the PyS60 source code tree.

use strict;

print " - Looking for .mmp files\n";

my @files = `dir /s /b *mmp`;
if (scalar @files < 1) {
   print "No .mmp files found!\n";
   exit 1;
}
chomp @files;

print " - Scanning ", scalar @files, " files\n";

foreach my $fn (@files) {
   my $contents = '';
   open(IN, $fn) || die "$fn: $!\n";
   while(<IN>) {
      $contents .= $_;
   }
   close IN;
   
   if (index($contents, 'GCCE') < 0) {
      if ($contents =~ /^(\s*#\s*(?:el)?if\s+defined\s*.\s*ARMCC.+?(deffile.+?)$)/ism) {
         my $armcc_def = $1;
         my $deffile   = $2;
         my $ofs = index($contents, $armcc_def) + length($armcc_def);
         print " > $fn\n";
         open(OUT, ">$fn") || die ">$fn: $!\n";
         print OUT substr($contents, 0, $ofs);
         print OUT "\n#elif defined(GCCE)\n$deffile";
         print OUT substr($contents, $ofs);
         print OUT "\nEPOCALLOWDLLDATA\n";
         close OUT;
      }
   }
}
